﻿using JESAI.Core.Reflection;
using System;
using System.Linq;

namespace JESAI.Core.Modularity
{
    /// <summary>
    /// JESAI 模块类型查找器
    /// </summary>
    public class JESAIModuleTypeFinder : BaseTypeFinderBase<JESAIModule>, IJESAIModuleTypeFinder
    {
        /// <summary>
        /// 初始化一个<see cref="JESAIModuleTypeFinder"/>类型的实例
        /// </summary>
        /// <param name="allAssemblyFinder">所有程序集查找器</param>
        public JESAIModuleTypeFinder(IAllAssemblyFinder allAssemblyFinder) : base(allAssemblyFinder)
        {
        }

        /// <summary>
        /// 重写已实现所有项的查找
        /// </summary>
        protected override Type[] FindAllItems()
        {
            // 排除被继承的Module实类
            var types = base.FindAllItems();
            var baseModuleTypes = types.Select(x => x.BaseType)
                .Where(x => x != null && x.IsClass && !x.IsAbstract)
                .ToArray();
            return types.Except(baseModuleTypes).ToArray();
        }
    }
}
